﻿@using KRM.Core
@model KRM.Core.vProductOption
@{
    var db = new KRMEntities();
    var price = (Model.ProductOptionDiscountPrice > 0) ? Model.ProductOptionDiscountPrice : Model.ProductOptionBasePrice;
    var productOptionAvailability = db.vProductOptionAvailabilities.FirstOrDefault(a => a.ProductOptionID == Model.ProductOptionID);
    var stock = "";
    var availability = "";
    if (productOptionAvailability != null)
    {
        if (productOptionAvailability.Availability.HasValue)
        {
            var stockLevel = productOptionAvailability.Availability.Value;

            if (stockLevel != 0)
            {
                if (stockLevel < 10)
                {
                    stock = "<span style=\"color: red; font-weight: bold\">Stock Level: " + stockLevel + "</span>";
                }
                else
                {
                    stock = "<span style=\"color: Black; \">Stock Level: " + stockLevel + "</span>";
                }
                availability = stockLevel.ToString();
            }
            else
            {
                var stockDate = db.spGetExpectedArrivalDate(Model.ProductOptionID).ToString();

                if (stockDate.Length > 5)
                {
                    stockDate = stockDate.Substring(0, 5);
                    stock = "<span style=\"color: Orange; font-weight: bold\">Stock Due: " + stockDate + "</span>";
                }
                else
                {
                    stock = "<span style=\"color: Red; font-weight: bold\">No Stock:No Due Date</span>";
                }
                availability = "0";
            }
        }
        else
        {
            stock = "<span>Can't check Stock(DD)</span>";
            availability = "0";
        }
    }

    var productSubOptions = db.vProductSubOptions.Where(a => a.ProductOptionID == Model.ProductOptionID).OrderBy(a => a.ProductSubOptionID).ToList();
}
<input type="hidden" name="Optiondays" value="@(Model.OptionDays.HasValue ? Model.OptionDays.Value : 14)"/>
<input type="hidden" id="hfProductOptionId" value="@Model.ProductOptionID"/>
<div id="pnSubOptions">
    @foreach (var item in productSubOptions.Distinct(so => so.ProductSubOptionID))
    {
        var subOptionItems = productSubOptions.Where(so => so.ProductSubOptionID == item.ProductSubOptionID).OrderBy(so => so.ProductSubOptionItemPrice).ToList();
        if (subOptionItems.Any())
        {
            <p>@subOptionItems.FirstOrDefault().ProductSubOptionName</p>
        }
        if (subOptionItems.FirstOrDefault() != null && subOptionItems.FirstOrDefault().ProductSubOptionOptional)
        {
            subOptionItems.Insert(0, new vProductSubOption()
            {
                ProductSubOptionItemID = 0, 
                ProductSubOptionItemPrice = 0,
                SubOptionTextWith = "Not required"
                            
            });
        }
        <div class="row-fluid">
            <select class="input-block-level" onchange="neworderproductsuboptionchange()" name="productSubOptionItemId">
                @foreach (var subOptionitem in subOptionItems)
                {
                    <option value="@(subOptionitem.ProductSubOptionItemID)_@subOptionitem.ProductSubOptionItemPrice">@subOptionitem.SubOptionTextWith</option>
                }
            </select>
        </div>
    }
</div>
<input type="hidden" id="hfcurrentPrice" value="@price"/>
<div class="row-fluid">
    <div class="span1">Price</div>
    <div class="span2">
        <input type="text" class="input-block-level" id="currentprice" value="£@price.ToString("N2")" readonly="readonly" name="price"/>
    </div>
    <div class="span2 text-right">Est LeadTime:</div>
    <div class="span1">
        <input type="text" class="input-block-level" disabled="disabled" value="@(Model.OptionDays.HasValue ? Model.OptionDays.Value.ToString() : "0")" />
    </div>
    <div class="span2 text-right">Availability:</div>
    <div class="span1">
        <input type="text" class="input-block-level" value="@availability" disabled="disabled" />
    </div>
    <div class="span3">
        @Html.Raw(stock)
    </div>
</div>
